<?php
require_once 'config.php';

$key = "s";
while($key != "x")
{
	main($key);
	showmenu();
    fwrite(STDOUT, "\r\nPlease input your select:");
    $key = trim(fgets(STDIN));
}

echo "bye";

function main($key){
	switch ($key) {
	case "s":
		showconfig();
		break;
	case "b":
		backupwebroot();
		break;
	case "b1":
		backupwebroot1();
		break;
	case "d":
		downloadf();
		break;
	case "c":
		changefolderf();
		break;
	case "uz":
		unzipf();
		break;
	case "up":
		upload();
		break;
	case "upl":
		upload2();
		break;
	case "init":
		initpath();
		break;
	case "bmy":
		backupmysql();
		break;
	}
}


function showmenu(){

	echo "==============================\r\n";
	echo "\ts\t:Show config\r\n";
	echo "\td\t:download\r\n";
	echo "\tuz\t:unzip\r\n";
	echo "\tup\t:upload webroot\r\n";
	echo "\tup1\t:upload webroot_1\r\n";
	echo "\tb\t:backup webroot\r\n";
	echo "\tb1\t:backup webroot to _1\r\n";
	echo "\tc\t:change webroot vs _1\r\n";
	echo "\tinit\t:init path\r\n";
	echo "\tbmy\t:backup mysql\r\n";
	echo "\tx\t:Exit\r\n";
	echo "==============================\r\n";
}

function downloadf(){
	global $httpfile,$root,$uploadfolder,$webroot,$backup,$upfilename;
	echo "$httpfile:";
	$upfilename = trim(fgets(STDIN));
	download($httpfile.$upfilename,$uploadfolder);
}

function unzipf(){
	global $httpfile,$root,$uploadfolder,$webroot,$backup,$upfilename,$configpath;
	if(empty($upfilename)) {
		echo "Error:upfile is not exists\r\n";
		return -1;
	}

	$f = explode(".",$upfilename);
	$f = $f[0];
	//echo $f;
	unzip($uploadfolder.$upfilename,$uploadfolder.$f."\\");
	pause();

	xcopy($configpath,$uploadfolder.$f."\\",1);
	echo "copy config ";
}

function upload(){
	global $httpfile,$root,$uploadfolder,$webroot,$backup,$upfilename;
	$f = explode(".",$upfilename);
	$f = $f[0];
	xcopy($uploadfolder.$f."\\",$webroot,1);
	pause();
}

function changefolderf(){
	global $webroot;

	$len=strlen($webroot); 
	$newstr=substr($webroot,0,$webroot-1); 

	changefolder($newstr,$newstr."_1");
}
function showconfig(){
	global $httpfile,$root,$uploadfolder,$webroot,$backup,$upfilename,$configpath;

	echo "httpfile $httpfile\r\n";
	echo "root $root\r\n";
	echo "uploadfolder $uploadfolder\r\n";
	echo "webroot $webroot\r\n";
	echo "backup $backup\r\n";
	echo "upfilename $upfilename\r\n";
	echo "configpath $configpath\r\n";
}

function backupwebroot(){
	echo "begin backup\r\n";
	global $httpfile,$root,$uploadfolder,$webroot,$backup;
	$bakpath = $backup."web\\";
	mkdir_f($bakpath);
	$bakpath = $bakpath.date("YmdH");
	xcopy($webroot,$bakpath,1);
	echo "backup end to path:$bakpath\r\n";
	pause();
}

function backupwebroot1(){
	echo "begin backup to _1\r\n";
	global $httpfile,$root,$uploadfolder,$webroot,$backup;
	$len=strlen($webroot); 
	$newstr=substr($webroot,0,$webroot-1)."_1\\"; 
	$rs = del_dir($newstr);
	//var_dump($rs);
	mkdir_f($newstr);
	xcopy($webroot,$newstr,1);
	echo "backup end to path:$newstr\r\n";
	pause();
}

function backupmysql(){
	echo "begin backup mysql\r\n";
	global $httpfile,$root,$uploadfolder,$webroot,$backup;
	$bakpath = $backup."db\\";
	mkdir_f($bakpath);
	$sqlpath = $bakpath."data.sql";
	$rarpath = $bakpath.date("YmdH").".rar";
	mysql_backup($sqlpath);
	zip_f($rarpath,$sqlpath);
	unlink($sqlpath);
	echo "backup end to path:$sqlpath\r\n";
	pause();

}

function pause(){
	echo "any key to continue...";
	fgets(STDIN);
}

function initpath(){
	global $httpfile,$root,$uploadfolder,$configpath,$backup;

	echo "init path $root config\r\n";
	mkdir_f($root."config");
	mkdir_f($root."webroot");
	mkdir_f($root."logs");

	echo "init path $uploadfolder\r\n";
	mkdir_f($uploadfolder);
	echo "init path $backup\r\n";
	mkdir_f($backup);
	echo "init path $configpath\r\n";
	mkdir_f($configpath);
	echo "init path end\r\n";
	
}